package club.chenlinghong.product.controller;

import club.chenlinghong.product.dto.CartDTO;
import club.chenlinghong.product.entity.ProductCatagory;
import club.chenlinghong.product.entity.ProductInfo;
import club.chenlinghong.product.service.CatagoryService;
import club.chenlinghong.product.service.ProductService;
import club.chenlinghong.product.util.ResultVOUtil;
import club.chenlinghong.product.vo.ProductInfoVO;
import club.chenlinghong.product.vo.ProductVO;
import club.chenlinghong.product.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 商品
 * @Author chenlinghong
 * @Date 2019/6/18 16:57
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CatagoryService catagoryService;

    /**
     * 1.查询所有在架商品
     * 2.获取类目type数目
     * 3.查询类目
     * 4.构造数据
     */
    @GetMapping(value = "/list")
    public ResultVO<ProductVO> list() {

        List<ProductInfo> productInfoList = productService.findUpAll();
        List<Integer> catagoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCatagoryType)
                .collect(Collectors.toList());
        List<ProductCatagory> productCatagories = catagoryService.findByCatagoryTypeIn(catagoryTypeList);

        // 构造数据
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCatagory productCatagory : productCatagories) {
            ProductVO productVO = new ProductVO();
            productVO.setCatagoryName(productCatagory.getCatagoryName());
            productVO.setCatagoryType(productCatagory.getCatagoryType());

            List<ProductInfoVO> productInfoVOS = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCatagoryType().equals(productCatagory.getCatagoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOS.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOS);
            productVOList.add(productVO);
        }
        return ResultVOUtil.success(productVOList);
    }

    /**
     * 获取product列表
     * [订单服务调用]
     *
     * @param productIdList
     * @return
     */
    @PostMapping(value = "/listForOrder")
    public List<ProductInfo> listForProduct(@RequestBody List<String> productIdList) {
        return productService.findList(productIdList);
    }

    /**
     * 扣库存
     *
     * @param cartDTOList
     */
    @PostMapping(value = "/decreaseStock")
    public void decreaseStock(@RequestBody List<CartDTO> cartDTOList) {
        productService.decreaseStock(cartDTOList);
    }
}
